package com.example.gamestore.controller;

import com.example.gamestore.pojo.Order;
import com.example.gamestore.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 创建订单
     * @param order 订单对象
     * @return 包含操作结果的 Map
     */
    @PostMapping("/create")
    public Map<String, Object> createOrder(@RequestBody Order order) {
        Order createdOrder = orderService.createOrder(order);
        Map<String, Object> result = new HashMap<>();
        if (createdOrder != null) {
            result.put("message", "订单创建成功");
            result.put("order", createdOrder);
        } else {
            result.put("message", "订单创建失败");
        }
        return result;
    }

    /**
     * 根据用户 ID 查询订单列表
     * @param userId 用户 ID
     * @return 订单列表
     */
    @GetMapping("/getByUserId/{userId}")
    public java.util.List<Order> getOrdersByUserId(@PathVariable Long userId) {
        return orderService.getOrdersByUserId(userId);
    }

    // 获取所有订单
    @GetMapping("/getAll")
    public List<Order> getAllOrders() {
        return orderService.getAllOrders();
    }

    // 根据订单 ID 获取订单
    @GetMapping("/getById/{id}")
    public Order getOrderById(@PathVariable Long id) {
        return orderService.getOrderById(id);
    }

    // 更新订单
    @PutMapping("/update")
    public String updateOrder(@RequestBody Order order) {
        int result = orderService.updateOrder(order);
        if (result > 0) {
            return "更新成功";
        } else {
            return "更新失败";
        }
    }

    // 删除订单
    @DeleteMapping("/delete/{id}")
    public String deleteOrder(@PathVariable Long id) {
        int result = orderService.deleteOrder(id);
        if (result > 0) {
            return "删除成功";
        } else {
            return "删除失败";
        }
    }

}